<?php

namespace console\db;

use yii\db\Query;
use yii\db\Expression;
use yii\helpers\ArrayHelper;

/**
 * 增量数据迁移批次任务信息记录
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class MigrationBatchRecord extends Migration {

    /**
     * @inheritdoc
     */
    public $name;

    /**
     * @inheritdoc
     */
    public $tableName = 'migration_batch';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $sourceDb = $this->getOriginalDb();
        $dbname = $this->getDbName($sourceDb);
        $_lastBatchId = (new Query())->from('{{%' . $this->tableName . '}}')->select(new Expression('max(id)'))->scalar();
        $lastBatchId = intval($_lastBatchId);
        $this->insert('{{%' . $this->tableName . '}}', [
            'name' => '[' . date('Y-m-d H:i:s') . ']批处理任务',
            'description' => $this->name,
            'created_at' => time(),
            'updated_at' => time(),
        ]);
        $batchId = $this->db->getLastInsertID();
        $tables = $this->recordTables();
        foreach ($tables as $targetTable => $sourceOptions) {
            $sourceTable = ArrayHelper::getValue($sourceOptions, 'table');
            $column = ArrayHelper::getValue($sourceOptions, 'column');
            if (!$sourceTable || !$column) {
                continue;
            }
//            $autoIncrement = ArrayHelper::getValue($sourceOptions, 'auto');
//            if ($autoIncrement) {
//                $nextId = (new Query())->from('information_schema.`TABLES`')->select('Auto_increment')->andWhere([
//                    'Table_Schema' => $dbname,
//                    'table_name' => $sourceTable,
//                ])->scalar($sourceDb);
//            } else {
            $maxColumnId = (new Query())->from('{{%' . $sourceTable . '}}')->select(new Expression('max(' . $column . ')'))->scalar($sourceDb);
            $nextId = intval($maxColumnId) + 1;
//            }
            $_lastBatchTableEnd = (new Query())->from('{{%' . $this->tableName . '_table}}')->select('increment_end')->andWhere([
                'batch_id' => $lastBatchId,
                'table' => $targetTable,
            ])->scalar();
            $this->insert('{{%' . $this->tableName . '_table}}', [
                'batch_id' => $batchId,
                'pre_batch_id' => $lastBatchId,
                'table' => $targetTable,
                'increment_start' => intval($_lastBatchTableEnd),
                'increment_end' => intval($nextId),
                'increment_column' => $column,
                'created_at' => time(),
                'updated_at' => time(),
            ]);
        }
    }

    protected function recordTables() {
        return [
            'shop_attachment' => [
                'table' => 'album_pic',
                'column' => 'apic_id',
                'auto' => true,
            ],
            'shop_order' => [
                'table' => 'orders',
                'column' => 'order_id',
                'auto' => true,
            ],
            'user_address' => [
                'table' => 'address',
                'column' => 'address_id',
                'auto' => true,
            ],
            'shop_product' => [
                'table' => 'goods_common',
                'column' => 'goods_commonid',
                'auto' => true,
            ],
            'shop_goods' => [
                'table' => 'goods',
                'column' => 'goods_id',
                'auto' => true,
            ],
            'shop_order_refund' => [
                'table' => 'refund_return',
                'column' => 'refund_id',
                'auto' => true,
            ],
            'shop_evaluate' => [
                'table' => 'evaluate_goods',
                'column' => 'geval_id',
                'auto' => true,
            ],
        ];
    }

}
